<?php

namespace app\api\admin\v1\controller;

use app\api\admin\Controller;
use extend\bridgecli\BridgClient;
use extend\bridgecli\BridgeProxy;
use zhanshop\App;
use zhanshop\server\Request;

class Proxy extends Controller
{
    /**
     * @api TCP socks5Message/{event}/{tofd} socks5代理访问消息
     * @apiGroup 代理访问
     * @param Request $request
     * @param \Swoole\Coroutine\Channel $response
     */
    public function socks5Message(Request $request, mixed $response)
    {
        $event = $request->getData('event');
        $tofd = intval($request->getData('tofd'));
        $data = $request->post();
        $bridgClient = App::make(BridgClient::class);
        if($event == "connect"){
            App::make(BridgeProxy::class)->socks5Connect($bridgClient, $tofd, $data);
        }elseif($event == "message" && $data && is_string($data)){
            App::make(BridgeProxy::class)->socks5Message($bridgClient, $tofd, $data);
        }elseif ($event == "close"){
            App::make(BridgeProxy::class)->socks5Close($bridgClient, $tofd);
        }
    }

    /**
     * @api TCP httpMessage/{event}/{tofd} http代理访问消息
     * @apiGroup 代理访问
     * @param Request $request
     * @param \Swoole\Coroutine\Channel $response
     */
    public function httpMessage(Request $request, mixed $response)
    {
        $event = $request->getData('event');
        $tofd = intval($request->getData('tofd'));
        $data = $request->post();
        $bridgClient = App::make(BridgClient::class);
        if($event == "connect"){
            App::make(BridgeProxy::class)->httpConnect($bridgClient, $tofd, $data);
        }elseif($event == "message" && $data && is_string($data)){
            App::make(BridgeProxy::class)->httpMessage($bridgClient, $tofd, $data);
        }elseif ($event == "close"){
            App::make(BridgeProxy::class)->httpClose($bridgClient, $tofd);
        }
    }
}